package cn.stylefeng.guns.modular.addressbook.controller;

import cn.stylefeng.guns.core.consts.ProjectConstants;
import cn.stylefeng.guns.modular.addressbook.entity.Department;
import cn.stylefeng.guns.modular.addressbook.pojo.request.DepartmentRequest;
import cn.stylefeng.guns.modular.addressbook.pojo.response.TreeDepartmentTypeInfo;
import cn.stylefeng.guns.modular.addressbook.service.DepartmentService;
import cn.stylefeng.guns.modular.addressbook.service.TreeDepartmentService;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 控制器
 *
 * @author xbz
 * @date 2023/04/27 09:19
 */
@RestController
@ApiResource(name = "")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    @Resource
    private TreeDepartmentService treeDepartmentService;

    /**
     * 添加
     *
     * @author xbz
     * @date 2023/04/27 09:19
     */
    @PostResource(name = "添加", path = "/department/add",requiredPermission = false,requiredLogin = false)
    public ResponseData<Department> add(@RequestBody @Validated(DepartmentRequest.add.class) DepartmentRequest departmentRequest) {
        departmentService.add(departmentRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 删除
     *
     * @author xbz
     * @date 2023/04/27 09:19
     */
    @PostResource(name = "删除", path = "/department/delete",requiredPermission = false,requiredLogin = false)
    public ResponseData<?> delete(@RequestBody @Validated(DepartmentRequest.delete.class) DepartmentRequest departmentRequest) {
        departmentService.del(departmentRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 编辑
     *
     * @author xbz
     * @date 2023/04/27 09:19
     */
    @PostResource(name = "编辑", path = "/department/edit",requiredPermission = false,requiredLogin = false)
    public ResponseData<?> edit(@RequestBody @Validated(DepartmentRequest.edit.class) DepartmentRequest departmentRequest) {
        departmentService.edit(departmentRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 查看详情
     *
     * @author xbz
     * @date 2023/04/27 09:19
     */
    @GetResource(name = "查看详情", path = "/department/detail",requiredPermission = false,requiredLogin = false)
    public ResponseData<Department> detail(@Validated(DepartmentRequest.detail.class) DepartmentRequest departmentRequest) {
        return new SuccessResponseData<>(departmentService.detail(departmentRequest));
    }

    /**
     * 获取列表
     *
     * @author xbz
     * @date 2023/04/27 09:19
     */
    @GetResource(name = "获取列表", path = "/department/list",requiredPermission = false,requiredLogin = false)
    public ResponseData<List<Department>> list(DepartmentRequest departmentRequest) {
        return new SuccessResponseData<>(departmentService.findList(departmentRequest));
    }

    /**
     * 获取列表（带分页）
     *
     * @author xbz
     * @date 2023/04/27 09:19
     */
    @GetResource(name = "分页查询", path = "/department/page",requiredPermission = false,requiredLogin = false)
    public ResponseData<PageResult<Department>> page(DepartmentRequest departmentRequest) {
        return new SuccessResponseData<>(departmentService.findPage(departmentRequest));
    }

    @GetResource(name = "分页查询", path = "/department/getTreeDepartmentList",requiredPermission = false,requiredLogin = false)
    public ResponseData<List<TreeDepartmentTypeInfo>> getTreeDepartmentList(DepartmentRequest departmentRequest) {
        String tableName = ProjectConstants.EPC_DEPARTMENT;
        return new SuccessResponseData<>(treeDepartmentService.getTreeDepartmentList(tableName));
    }

}
